package com.zxmzy.scaffold.core.loading

import android.app.Activity
import android.content.Context
import android.text.TextUtils

/**
 * @author zhangxiaoming
 * @date 2023/7/28
 * @description 等待提示框
 */
class LoadingManager(private val mContext: Context) {
    private var loadView: CenterLoadingDialog? = null

    /**
     * 统一耗时操作Dialog
     */
    fun showLoading(text: String? = null) {
        if (loadView == null) {
            loadView = CenterLoadingDialog(mContext)
        }
        if (loadView?.isShowing == true) {
            loadView?.dismiss()
        }
        if (!TextUtils.isEmpty(text)) {
            loadView?.setTitle(text as CharSequence)
        }
        if (mContext is Activity && mContext.isFinishing) {
            return
        }
        loadView?.show()
    }

    /**
     * 关闭Dialog
     */
    fun dismissLoading() {
        if (mContext is Activity && mContext.isFinishing) {
            return
        }
        loadView?.let {
            if (it.isShowing) {
                it.dismiss()
            }
        }
    }
}